package com.ze.familydayverpm.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.mouse.customview.JazzyViewPager;
import com.mouse.customview.OutlineContainer;
import com.ze.familydayverpm.fragment.PicFragment;
import com.ze.familydayverpm.fragment.RelativeLayoutImp;
import com.ze.familydayverpm.fragment.SubscribeFragment;
import com.ze.model.PhotoModel;


import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * @author fangwuze
 * 相册的适配器.
 */
public class SpaceViewPagerAdapter extends PagerAdapter {
	/**
	 * 上下文
	 */
	private Context mContext;
	
	/**
	 * 数据源,这里是JSONARRAY
	 */
	private List<PhotoModel> mListJSON ;
	
	/**
	 * Hashmap保存相片的位置以及ItemView.
	 */
	public HashMap<Integer, RelativeLayoutImp> mHashMap;
	private int mCount = 0;
	
	public SpaceViewPagerAdapter(Context context,List<PhotoModel> mListJSON/*, JazzyViewPager viewPager*/) {
		this.mContext = context;
		this.mListJSON = mListJSON;
		mHashMap = new HashMap<Integer, RelativeLayoutImp>();
		//viewPager.setmObjs(mHashMap);
	}
	
	//这里进行回收，当我们左右滑动的时候，会把早期的图片回收掉.
	@Override
	public void destroyItem(View container, int position, Object object) {
		RelativeLayoutImp itemView = mHashMap.get(position);
		itemView.recycle();
		((ViewPager)container).removeView((View)mHashMap.get(position));
		mHashMap.remove(position);
		/*RelativeLayoutImp itemView = mListView.get(position);
		itemView.recycle();
		((ViewPager)container).removeView((View)mListView.get(position));*/
	}
	
	@Override
	public void finishUpdate(View view) {

	}

	//这里返回相册有多少条,和BaseAdapter一样.
	@Override
	public int getCount() {
		return mListJSON == null ? 0 : mListJSON.size();
	}
	
	//这里就是初始化ViewPagerItemView.如果ViewPagerItemView已经存在,
	//重新reload，不存在new一个并且填充数据.
	@Override
	public Object instantiateItem(View container, int position) {
		RelativeLayoutImp itemView = null ;
		if(mHashMap.containsKey(position)){
			itemView = mHashMap.get(position);
			System.out.println("containsKey:" + position);
			itemView.reload();
		}else{
			PhotoModel dataObj = mListJSON.get(position);
			String type = null;
			if(dataObj.type != null)
			{
				type = dataObj.type;
			}
			if (type.equals("photoid") || type.equals("rephotoid") ) {
				itemView = new PicFragment(mContext);
			}else if (type.equals("blogid") || type.equals("reblogid")) {
				itemView = new SubscribeFragment(mContext);
			}
			itemView.setData(dataObj);
			mHashMap.put(position, itemView);
		}
		((ViewPager) container).addView((View)itemView);
		return itemView;
	}
	@Override
	public boolean isViewFromObject(View view, Object obj) {
		if (view instanceof OutlineContainer) {
			return ((OutlineContainer) view).getChildAt(0) == obj;
		} else {
			return view == obj;
		}
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View view) {

	}
	public void notifyListView( int index )
	{
		mHashMap.get(index).reload();
	}

	@Override
	public int getItemPosition(Object object) {
		if (mCount > 0) {
			mCount -- ;
			return POSITION_NONE;
		}
		return super.getItemPosition(object);
	}

	public void notifyDataSetChanged(int index) {
		mCount = getCount();
		notifyDataSetChanged();
	}
	
}
